package com.springboot.service.mq.producer;

import com.springboot.rmq.comm.Content;
import com.springboot.utils.JsonUtils;
import com.springboot.rmq.comm.Selector;
import com.springboot.rmq.producer.MsgProducer;
import com.springboot.rmq.producer.bean.RmqMessage;
import com.springboot.rmq.producer.manager.MsgProducerManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


@Component("UserInfoMQ")
public class UserInfoMQ {
    /**
     * 日志
     */
    private static Log logger = LogFactory.getLog("awardLog");

    private static Log awardErrorLogger = LogFactory.getLog("awardErrorLog");

    /**
     * 创建消息生产者的工厂类
     */
    @Resource(name = "producerManager")
    private MsgProducerManager msgProducerManager;

    /**
     * 消息生产者
     */
    private MsgProducer msgProducer;

    @PostConstruct
    public void MsgProducer() {

        this.msgProducer = msgProducerManager.createProducer("prizesInfoRmq");
    }

    public void awardCredit(Content content) {

        RmqMessage message = new RmqMessage();
        message.setKey(content.getKey());
        message.setTopic("giftInfo");
        message.setTags("gift");
        message.setBody(JsonUtils.toJson(content));
        message.setOrder(true);

        try {
            logger.info("生产发积分奖品通知消息体：" + message.getBody());
            msgProducer.sendAsyncMsgOrder(
                    message,
                    Selector.INSTANCE,
                    message.getKey(),
                    new SendCallBack()
            );
        } catch (Exception e) {
            awardErrorLogger.error("生产发积分奖品通知消息中心失败：" + message.getBody(), e);
        }
    }

}
